﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Controls;

namespace Fluent
{
    /// <summary>
    /// Represents separator to use in the TabControl
    /// </summary>
    public class SeparatorTabItem : TabItem
    {
        #region Constructors

        /// <summary>
        /// Static constructor
        /// </summary>
        [SuppressMessage("Microsoft.Performance", "CA1810")]
        static SeparatorTabItem()
        {
            Type type = typeof (SeparatorTabItem);
            DefaultStyleKeyProperty.OverrideMetadata(type, new FrameworkPropertyMetadata(type));
            IsEnabledProperty.OverrideMetadata(type,
                                               new FrameworkPropertyMetadata(false, null, CoerceIsEnabledAndTabStop));
            IsTabStopProperty.OverrideMetadata(type,
                                               new FrameworkPropertyMetadata(false, null, CoerceIsEnabledAndTabStop));
            IsSelectedProperty.OverrideMetadata(type, new FrameworkPropertyMetadata(false, OnIsSelectedChanged));
            StyleProperty.OverrideMetadata(typeof (SeparatorTabItem), new FrameworkPropertyMetadata(null, OnCoerceStyle));
        }

        // Coerce object style
        private static object OnCoerceStyle(DependencyObject d, object basevalue)
        {
            if (basevalue == null)
            {
                basevalue = (d as FrameworkElement).TryFindResource(typeof (SeparatorTabItem));
            }

            return basevalue;
        }

        private static void OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(bool) e.NewValue) return;
            var separatorTabItem = (SeparatorTabItem) d;
            var tabControl = separatorTabItem.Parent as TabControl;
            if (tabControl == null || tabControl.Items.Count <= 1) return;
            tabControl.SelectedIndex = tabControl.SelectedIndex == tabControl.Items.Count - 1
                                           ? tabControl.SelectedIndex - 1
                                           : tabControl.SelectedIndex + 1;
        }

        private static object CoerceIsEnabledAndTabStop(DependencyObject d, object basevalue)
        {
            return false;
        }

        #endregion
    }
}